﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.DB;
using RMS.Common;
using System.IO;
using System.Diagnostics;

namespace RMS
{
    public partial class DialogBarcodePrintPreview : Form
    {


        public DialogBarcodePrintPreview(BarcodeTemplate templateBarcode, List<Inventory> listProductPrint)
        {
            
            InitializeComponent();

            //Set Top Most
            TopMost = Constants.FullScreen();

            //Show items
            ShowBarcodeList(templateBarcode, listProductPrint);
            
        }

        private void ShowBarcodeList(BarcodeTemplate templateBarcode, List<Inventory> listProductPrint)
        {
            int numerOfCopy = 0;
            

            foreach (Inventory item  in listProductPrint)
            {
                if (templateBarcode.PrintAll)
                {
                    numerOfCopy = item.Quantity.Value;
                }
                else
                {
                    numerOfCopy = templateBarcode.PrintCopyNumber;
                }

                for (int i = 0; i < numerOfCopy; i++)
                {
                    BarCodeCtrl barcode = new BarCodeCtrl()
                    {
                        Name = item.ID+i,
                        ShowHeader = templateBarcode.ShowHeader,
                        HeaderFont = templateBarcode.HeaderFont,
                        ShowFooter = templateBarcode.ShowFooter,
                        FooterFont = templateBarcode.FooterFont,
                        BarCodeHeight = templateBarcode.BarCodeHeight,
                        Weight = templateBarcode.Weight,
                        Size = templateBarcode.DefaultSize,

                    };

                    if (barcode.ShowHeader)
                    {
                        StringBuilder headerText = new StringBuilder();
                        if (templateBarcode.ShowProductName)
                        {
                            headerText.Append(item.Name);
                        }

                        if (templateBarcode.ShowPrice)
                        {
                            if (templateBarcode.ShowProductName && !string.IsNullOrEmpty(item.Name))
                            {
                                headerText.Append("|");
                            }
                            headerText.Append(Utilities.FormatNumber(item.SalePrice.Value, Utilities.PrecisionFormat));
                        }

                        barcode.HeaderText = headerText.ToString();
                    }

                    if (barcode.ShowFooter)
                    {
                        StringBuilder footerText = new StringBuilder();
                        footerText.Append(item.ID);

                        barcode.BarCode = footerText.ToString();
                    }

                    barcode.ResumeLayout();
                    barcode.Update();
                    barcode.Show();
                    flowLayoutPanel1.Controls.Add(barcode);
                }
            }

            flowLayoutPanel1.Show();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnExportBarcode_Click(object sender, EventArgs e)
        {
            string temp = "";
            string filePrintTemplate = Environment.CurrentDirectory + Constants.PrintBarcodeTemplateFileName;
            string newImageElementHtmlFormat = @"<img border=1 src={0} />";
            StringBuilder strHTML = new StringBuilder();
            StreamReader readerPrintTemplate = null;
            var barcodeFileName = "";
            string filePrintMe = "";
            try
            {
                if (File.Exists(filePrintTemplate))
                {
                    foreach (var control in flowLayoutPanel1.Controls)
                    {
                        //Save image file
                        
                        BarCodeCtrl barcode = (BarCodeCtrl)control;
                        if (barcode.BarCode != temp)
                        {
                            temp = barcode.BarCode;
                            barcodeFileName = String.Format("{0}{1}.jpg", barcode.Name, Utilities.UniqueString());
                            barcode.SaveImage(barcodeFileName);
                        }

                        //Genarate HTML string
                        strHTML.AppendFormat(newImageElementHtmlFormat, barcodeFileName);                        
                        
                    }

                    var printContent = "";
                    using (readerPrintTemplate = new StreamReader(filePrintTemplate))
                    {
                        printContent = readerPrintTemplate.ReadToEnd();
                    }

                    printContent = printContent.Replace("###DataToPrint###", strHTML.ToString());


                    filePrintMe = String.Format("{0}{1}\\Barcode_{2}.doc", Environment.CurrentDirectory, Constants.BarcodeFilePath, Utilities.UniqueString());
                    filePrintMe = Path.GetFullPath(filePrintMe);
                    using (StreamWriter writer = new StreamWriter(filePrintMe))
                    {
                        writer.Write(printContent);
                        writer.Close();
                    }

                    Process.Start(filePrintMe);
                    
                }
                else
                {
                    MessageBox.Show("Khong tim thay file " + Constants.PrintBarcodeTemplateFileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (readerPrintTemplate != null)
                {
                    readerPrintTemplate.Close();
                    readerPrintTemplate.Dispose();
                }
                
            }
            this.Close();
        }
    }
}
